Passed
Push — master ( 0aa252...47dfa4 )
by Dmytro
01:49 queued 10s
created

AtlassianApi   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 4
dl 0
loc 7
c 0
b 0
f 0
rs 10
1
2
import Api from 'base-api-client';
3
import API_ERROR from 'base-api-client/lib/Error';
4
import { dumpUser } from './utils/dumpUtils';
5
import defaultLogger from './logger';
6
7
export class ATLASSIAN_ERROR extends API_ERROR {
8
    get message() {
9
        const messages = [ this.payload.message ];
10
        const inner  = this.payload.response?.data;
11
12
        if (inner.message) {
13
            messages.push(inner.message);
14
        } else if (inner?.errorMessages?.length) {
15
            messages.push(...inner.errorMessages);
16
        } else if (inner) {
17
            messages.push(JSON.stringify(inner));
18
        }
19
20
        return messages.join(' ');
21
    }
22
}
23
24
25
export default class AtlassianApi extends Api {
26
    constructor(url, auth) {
27
        super(url);
28
        this.auth = auth;
29
    }
30
31
    onError(error) {
32
        if (error.isAxiosError) throw new ATLASSIAN_ERROR(error);
33
        throw error;
34
    }
35
36
    initLogger(logger = defaultLogger) {
37
        this.logger = logger;
38
    }
39
40
    async getMyself() {
41
        const res = await this.get('/rest/api/3/myself');
42
43
        return dumpUser(res);
44
    }
45
}
46